﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class AssortmentDetails : Form
    {
        private Util.ActionType _openAction;
        public Util.ActionType OpenAction
        {
            get
            { return this._openAction; }
            set
            { this._openAction = value; }
        }
        private DataSets.Dictionary.AssortmentDataTable _data;

        public DataSets.Dictionary.AssortmentDataTable Data
        {
            get { return _data; }
            set { _data = value; }
        }
        private DataSets.Dictionary.AssortmentRow _currentRow;

        public DataSets.Dictionary.AssortmentRow CurrentRow
        {
            get { return _currentRow; }
            set { _currentRow = value; }
        }
        private Int32 _currentAssortmentId;

        public Int32 CurrentAssortmentId
        {
            get { return _currentAssortmentId; }
            set { _currentAssortmentId = value; }
        }
	

        public AssortmentDetails(Util.ActionType openAction, DataSets.Dictionary.AssortmentDataTable data,Int32 currentAssotmentId)
        {
            InitializeComponent();
            this.Data = data;
            this.CurrentAssortmentId = currentAssotmentId;
            this.CurrentRow = data.FindById(this.CurrentAssortmentId);
            this.OpenAction = openAction;
        }

        private void AssortmentDetails_Load(object sender, EventArgs e)
        {
            InitComboBoxes();
            switch (this.OpenAction)
            {
                case DIProject.Util.ActionType.Insert:
                    InitInsert();
                    break;
                case DIProject.Util.ActionType.Update:
                    InitUpdate();
                    break;
                default:
                    this.Close();
                    break;
            }
        }
        private void InitComboBoxes()
        {
            cbAssesGroup.DataSource = DIProject.Data.AssortmentGroup.Data;
            cbCatalog.DataSource = DIProject.Data.Catalog.Data;
            cbMedForm.DataSource = DIProject.Data.MedForm.Data;
        }
        private void InitUpdate()
        {
            btnInsertUpdate.Text = "Изменить";
            btnDelete.Visible = true;
            this.txbName.Text = this.CurrentRow.Name;
            cbCatalog.SelectedValue = this.CurrentRow.CatalogId;
            this.cbMedForm.SelectedValue = this.CurrentRow.MedFormId;
            this.cbAssesGroup.SelectedValue = this.CurrentRow.AssesmentGroupId;
            this.txbListAB.Text = this.CurrentRow.ABList;
            this.txbNonPatentRus.Text = this.CurrentRow.NotPatentedRus;
            this.txbNonPatentLat.Text = this.CurrentRow.NotPatentedLat;
            if(!this.CurrentRow.IsGNVLSNull())
                this.cbxGNVLS.Checked = this.CurrentRow.GNVLS;            
            this.cbxMandatory.Checked = this.CurrentRow.Mandatory;
            this.txbTemperatureStore.Text = this.CurrentRow.StoreTemperature;
            if(!this.CurrentRow.IsBeforeOverdueNull())
                this.numericBeforeOverdue.Value = Convert.ToDecimal(this.CurrentRow.BeforeOverdue);
            if(!this.CurrentRow.IsNoRecipeNull())
                this.cbxNoRecipe.Checked = this.CurrentRow.NoRecipe;
            if(!this.CurrentRow.IsReferentPriceNull())
                this.txbReferentPrice.Text = this.CurrentRow.ReferentPrice.ToString();
            if(!this.CurrentRow.IsReferentDateNull())
                this.dtpReferentDate.Value = this.CurrentRow.ReferentDate;
            this.txbNote.Text = this.CurrentRow.Note;
        }
        private void InitInsert()
        {
            btnInsertUpdate.Text = "Сохранить";
            btnDelete.Visible = false;
            cbCatalog.SelectedValue = Util.Karantin.KarantinGroupId;
        }

        private void btnInsertUpdate_Click(object sender, EventArgs e)
        {
            switch (this.OpenAction)
            {
                case DIProject.Util.ActionType.Insert:
                    this.RunInsert();
                    break;
                case DIProject.Util.ActionType.Update:
                    this.RunUpdate();
                    break;
                default:
                    this.Close();
                    break;
            }
        }
        private void RunUpdate()
        {
            this.CurrentRow.Name = this.txbName.Text;
            this.CurrentRow.CatalogId = (Int32)this.cbCatalog.SelectedValue;
            this.CurrentRow.MedFormId = (Int32)this.cbMedForm.SelectedValue;
            this.CurrentRow.AssesmentGroupId = (Int32)this.cbAssesGroup.SelectedValue;
            this.CurrentRow.ABList = this.txbListAB.Text;
            this.CurrentRow.NotPatentedRus = this.txbNonPatentRus.Text;
            this.CurrentRow.NotPatentedLat = this.txbNonPatentLat.Text;
            this.CurrentRow.GNVLS = this.cbxGNVLS.Checked;
            this.CurrentRow.Mandatory = this.cbxMandatory.Checked;
            this.CurrentRow.StoreTemperature = this.txbTemperatureStore.Text;
            if (this.numericBeforeOverdue.Value > 0)
                this.CurrentRow.BeforeOverdue = Convert.ToByte(this.numericBeforeOverdue.Value);
            this.CurrentRow.NoRecipe = this.cbxNoRecipe.Checked;
            if (!string.IsNullOrEmpty(this.txbReferentPrice.Text))
            {
                decimal d;
                if (decimal.TryParse(this.txbReferentPrice.Text, out d))
                    this.CurrentRow.ReferentPrice = d;
                else
                    this.CurrentRow.SetReferentPriceNull();
            }
            else
            {
                this.CurrentRow.SetReferentPriceNull();
            }
            if (dtpReferentDate.Checked)
                this.CurrentRow.ReferentDate = this.dtpReferentDate.Value;
            else
                this.CurrentRow.SetReferentDateNull();
            this.CurrentRow.Note = this.txbNote.Text;
            this.CurrentRow.MW = FirstForm.Login;
            this.CurrentRow.MD = DateTime.Now;
            DataSets.DictionaryTableAdapters.AssortmentTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.AssortmentTableAdapter();
            adapter.Update(this.Data);
            this.CurrentRow.Original_MD = this.CurrentRow.MD;
            this.Data.AcceptChanges();
            this.Close();
        }
        private void RunInsert()
        {
            DataSets.Dictionary.AssortmentRow ar = this.Data.NewAssortmentRow();
            ar.Name = this.txbName.Text;
            //ar.CatalogId = Util.Karantin.KarantinGroupId;
            ar.CatalogId = (Int32)this.cbCatalog.SelectedValue;
            ar.MedFormId = (Int32)this.cbMedForm.SelectedValue;
            ar.AssesmentGroupId = (Int32)this.cbAssesGroup.SelectedValue;
            ar.ABList = this.txbListAB.Text;
            ar.NotPatentedRus = this.txbNonPatentRus.Text;
            ar.NotPatentedLat = this.txbNonPatentLat.Text;
            ar.GNVLS = this.cbxGNVLS.Checked;
            ar.Mandatory = this.cbxMandatory.Checked;
            ar.StoreTemperature = this.txbTemperatureStore.Text;
            if(this.numericBeforeOverdue.Value>0)
                ar.BeforeOverdue = Convert.ToByte(this.numericBeforeOverdue.Value);
            ar.NoRecipe = this.cbxNoRecipe.Checked;
            if (!string.IsNullOrEmpty(this.txbReferentPrice.Text))
            {
                ar.ReferentPrice = Convert.ToDecimal(this.txbReferentPrice.Text);
            }
            else
            {
                ar.SetReferentPriceNull();
            }
            if(dtpReferentDate.Checked)
                ar.ReferentDate = this.dtpReferentDate.Value;
            ar.Note = this.txbNote.Text;
            ar.MW = FirstForm.Login;
            ar.MD = DateTime.Now;            
            this.Data.Rows.Add(ar);
            DataSets.DictionaryTableAdapters.AssortmentTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.AssortmentTableAdapter();
            adapter.Update(this.Data);
            this.Data.AcceptChanges();
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                CurrentRow.Delete();
                DataSets.DictionaryTableAdapters.AssortmentTableAdapter adapter = new DIProject.DataSets.DictionaryTableAdapters.AssortmentTableAdapter();
                adapter.Update(this.Data);
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txbName_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                if (string.IsNullOrEmpty(tb.Text))
                {
                    e.Cancel = true;
                    tb.Focus();
                    MessageBox.Show("Заполните поле \"Наименование\"", "Необходимо", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void txbReferentPrice_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                if (string.IsNullOrEmpty(tb.Text))
                    return;
                decimal d;
                if (!decimal.TryParse(tb.Text, out d))
                {
                    e.Cancel = true;
                    tb.Focus();
                    MessageBox.Show("Введите дробное число", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }
    }
}